package rabbitutil

import (
	"encoding/json"
	"github.com/RaymondCode/simple-demo/config"
	"github.com/RaymondCode/simple-demo/entity/rabbitentity"
	"github.com/streadway/amqp"
)

const MANDATORY = true

// Favorite 点赞操作
func Favorite(userId, videoId int, isFavorite bool) error {
	channel, err := conn.Channel()
	if err != nil {
		return err
	}
	// 服务端声明
	if err := initFavorite(channel); err != nil {
		return err
	}
	var body = rabbitentity.Favorite{
		UserId:     userId,
		VideoId:    videoId,
		IsFavorite: isFavorite,
	}
	// 创建消息与管道
	rabbitMSG := rabbitentity.RabbitMSG[rabbitentity.Favorite]{
		Data:        body,
		ResendCount: 0,
		Type:        rabbitentity.FAVORITE,
	}
	return publish[rabbitentity.Favorite](&rabbitMSG,
		config.Config.Rabbit.Exchange.ServiceExchange,
		config.Config.Rabbit.Key.Favorite,
		channel)
}

func Follow(userId, toUserId int, isFollow bool) error {
	channel, err := conn.Channel()
	if err != nil {
		return err
	}
	// 服务端声明
	if err := initFollow(channel); err != nil {
		return err
	}
	var body = rabbitentity.Follow{
		UserId:   userId,
		ToUserId: toUserId,
		IsFollow: isFollow,
	}
	//创建消息与管道
	rabbitMSG := rabbitentity.RabbitMSG[rabbitentity.Follow]{
		Data:        body,
		ResendCount: 0,
		Type:        rabbitentity.FOLLOW,
	}
	return publish[rabbitentity.Follow](&rabbitMSG,
		config.Config.Rabbit.Exchange.ServiceExchange,
		config.Config.Rabbit.Key.Follow,
		channel)
}

// UploadVideo 上传视频文件
func UploadVideo(videoId int) error {
	channel, err := conn.Channel()
	if err != nil {
		return err
	}
	if err := initUploadVideo(channel); err != nil {
		return err
	}
	rabbitMSG := rabbitentity.RabbitMSG[int]{
		Data:        videoId,
		ResendCount: 0,
		Type:        rabbitentity.UPLOAD_VIDEO,
	}
	return publish[int](&rabbitMSG,
		config.Config.Rabbit.Exchange.ServiceExchange,
		config.Config.Rabbit.Key.UploadVideo,
		channel)
}

func FeedVideo(videoId int) error {
	channel, err := conn.Channel()
	if err != nil {
		return err
	}
	if err := initFeedVideo(channel); err != nil {
		return err
	}
	rabbitMSG := rabbitentity.RabbitMSG[int]{
		Data:        videoId,
		ResendCount: 0,
		Type:        rabbitentity.FEED_VIDEO,
	}
	return publish[int](&rabbitMSG,
		config.Config.Rabbit.Exchange.ServiceExchange,
		config.Config.Rabbit.Key.FeedVideo,
		channel)
}

// Publish 外部发布消息
func Publish[T rabbitentity.RabbitType](rabbitMSG *rabbitentity.RabbitMSG[T], exchange, key string) error {
	data, err := json.Marshal(rabbitMSG)
	if err != nil {
		return err
	}
	channel, err := conn.Channel()
	if err != nil {
		return err
	}
	// 发送消息
	err = channel.Publish(
		exchange,
		key,
		MANDATORY,
		false,
		amqp.Publishing{
			ContentType: "text/json",
			Body:        data,
		},
	)
	if err != nil {
		return err
	}
	return channel.Close()
}

// publish 外部发布消息
func publish[T rabbitentity.RabbitType](rabbitMSG *rabbitentity.RabbitMSG[T], exchange, key string, channel *amqp.Channel) error {
	data, err := json.Marshal(rabbitMSG)
	if err != nil {
		return err
	}
	// 发布消息
	err = channel.Publish(
		exchange,
		key,
		MANDATORY,
		false,
		amqp.Publishing{
			ContentType: "text/json",
			Body:        data,
		},
	)
	if err != nil {
		return err
	}
	return channel.Close()
}
